package com.wwj.admin.wawa.controller;

import com.github.pagehelper.Page;
import com.wwj.core.wawa.entity.WaWaType;
import com.wwj.core.wawa.service.IWaWaTypeService;
import com.wwj.framework.utils.Page2EasyUiData;
import com.wwj.framework.utils.ReturnData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Auser on 2017/11/15.
 */

@RequestMapping("/wawa/type")
@Controller
public class WaWaTypeControoler {

    @Resource
    private IWaWaTypeService waWaTypeService;

    private final String INDEX = "/admin/wawa/type";

    @RequestMapping(value = {"","/","/index"})
    public String index()throws Exception{
        return INDEX;
    }

    @RequestMapping("/listData.json")
    @ResponseBody
    public ReturnData list(String search,int offset,int limit)throws Exception{
        Map<String, Object> params = new HashMap<String, Object>();
        if(null != search && !"".equals(search)) params.put("search",search);
        Page<WaWaType> types = waWaTypeService.findByPage(params, offset, limit);
        return new ReturnData().success(Page2EasyUiData.format(types));
    }

    @RequestMapping("/findById/{id}")
    @ResponseBody
    public ReturnData findById(@PathVariable("id") int id)throws Exception{
        return new ReturnData().success(waWaTypeService.findById(id));
    }

    @RequestMapping("/update")
    @ResponseBody
    public ReturnData update(WaWaType waWaType)throws Exception{
        waWaTypeService.update(waWaType);
        return new ReturnData().success("ok");
    }

    @RequestMapping("/add")
    @ResponseBody
    public ReturnData add(WaWaType waWaType)throws Exception{
        waWaTypeService.add(waWaType);
        return new ReturnData().success("ok");
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    @ResponseBody
    public ReturnData deleteById(Integer id)throws Exception{
        return waWaTypeService.deleteById(id);
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public ReturnData findAll()throws Exception{
        return new ReturnData().success(waWaTypeService.findAll());
    }

}
